# -*- coding: utf-8 -*-

import matplotlib
matplotlib.use('Agg') 
import sys
sys.path.append('/opt/alps/lib')
sys.path.append('/share/opt/alps/lib')
import pyalps
import matplotlib.pyplot as plt
import pyalps.plot
import numpy as np
import time
import multiprocessing

#----------------------1 Run the Task-------------------

taskname = 'L=24_N=24_t=1_O=1_phi=0_U=5'
numcores = 4  ## used cores

Ls = [32,64]
N_totals = [1,2]
Omegas = np.arange(0.1,0.3,0.1)
phis = np.arange(0.1,0.3,0.1)
Us = np.arange(1.0,3.0,1.0)
Paras = []
for a1 in Ls:
    for a2 in N_totals:
        for a3 in Omegas:
            for a4 in phis:
                for a5 in Us:
                    Paras.append((a1,a2,a3,a4,a5))

def Runmps(para):
    (L,N_total,Omega,phi,U) = para
    tau = 10.0
    ns = 500
    dt = tau / ns
    parm = {}
    parm['LATTICE_LIBRARY'] = '0lattices.xml'
    parm['LATTICE'] = 'inhomogeneous open chain lattice'
    parm['MODEL_LIBRARY'] = "0models.xml"
    parm['MODEL'] = "spin tensor boson Hubbard"
    
    parm['CONSERVED_QUANTUMNUMBERS'] = 'N'
    parm['MAXSTATES'] = 100
    parm['Nmax'] = 2
    
    parm['L'] = L
    parm['N_total'] = N_total
    parm['t'] = 1
    parm['Omega'] = Omega
    parm['U0'] = U
    parm['U2'] = U*0.03
    parm['phi'] = phi
    
    parm['MEASURE_LOCAL[n_density]'] = 'n'
    parm['MEASURE_LOCAL[n_up_density]'] = 'n_up'
    parm['MEASURE_LOCAL[n_0_density]'] = 'n_0'
    parm['MEASURE_LOCAL[n_down_density]'] = 'n_down'
    
    parm['MEASURE_CORRELATIONS[n_up_correlation]'] = "bdag_up:b_up"
    parm['MEASURE_CORRELATIONS[n_0_correlation]'] = "bdag_0:b_0"
    parm['MEASURE_CORRELATIONS[n_down_correlation]'] = "bdag_down:b_down"

    parm['init_state'      ] = 'local_quantumnumbers'
    parm['initial_local_N'] = ','.join((['1.0'])* 24)
    parm['initial_local_Nup'] = ','.join((['0.0'])* 24)
    parm['initial_local_N0'] = ','.join((['1.0'])* 24)
    parm['initial_local_Ndown'] = ','.join((['0.0'])* 24)
    parm['te_order' ] = 'second'
    parm['DT'       ] = dt
    parm['TIMESTEPS'] = ns
    parm['tau'      ] = tau # not used in the simulation, but useful in the evaluation below
    parm['ALWAYS_MEASURE'] = 'n_density,n_up_density,n_0_density,n_down_density,n_up_correlation,n_0_correlation,n_down_correlation'
    parm['chkp_each'     ] = ns
    parm['measure_each'  ] = 5
    parm['COMPLEX'       ] = 1
#    parm['MEASURE_CORRELATIONS[Splus_Splus_Splus_Splus_correlations]'] ='SplusSplus'
    parm = [parm]

    ## write input files and run application
    input_file = pyalps.writeInputFiles(taskname + 'para=_' + str(para),parm)
    a1 = time.time()
    pyalps.runApplication('mps_evolve', input_file)
    a2 = time.time()
    print('run %s task has spent %.2f minutes' % (para, (a2 - a1)/60.0))

pool = multiprocessing.Pool(processes=numcores)
pool.map(Runmps, Paras)
pool.close()
pool.join()

#----------------------2 data_analysis--------------------------
##  still empty

